package com.mq.listener;

import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import sun.security.krb5.internal.crypto.EType;

import java.time.LocalTime;
import java.util.Map;

/**
 * @author zx
 * @date 2021/11/1 11:41
 */
@Component
@Slf4j
public class SpringRabbitListener {


    //Basic Queue
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg) throws Exception {
//        //System.out.println("spring 消费者接收到消息：【" + msg + "】");
//        log.info("spring 消费者接收到消息：【" + msg + "】");
//    }


    //workQueue
//    private static long work1Num1=0;
//    private static long work1Num2=0;
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue1(String msg) throws Exception {
//        work1Num1++;
//        log.warn("消费者1接收到消息：【" + msg + "】" + "---->"+LocalTime.now(),work1Num1);
//        Thread.sleep(200);
//    }
//
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue2(String msg) throws Exception {
//        work1Num1++;
//        log.error("消费者2接收到消息：【" + msg + "】" +"---->"+ LocalTime.now(),work1Num2);
//        Thread.sleep(300);
//    }
//



    //fanout
//    @RabbitListener(queues = "fanout.Queue1")
//    public void listenFanoutQueue1(String msg) throws Exception {
//        log.info("消费者1接收到消息：【" + msg + "】" +"---->"+ LocalTime.now());
//        Thread.sleep(300);
//    }
//    @RabbitListener(queues = "fanout.Queue2")
//    public void listenFanoutQueue2(String msg) throws Exception {
//        log.warn("消费者2接收到消息：【" + msg + "】" +"---->"+ LocalTime.now());
//        Thread.sleep(300);
//    }




    //direct
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue("direct.Queue1"),  //队列
//            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT), //交换机
//            key = {"blue","red"} //路由key
//    ))
//    public void listenDirectQueue1(String msg) throws Exception {
//        log.info("消费者1接收到direct消息：【" + msg + "】" +"---->"+ LocalTime.now());
//        Thread.sleep(300);
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue("direct.Queue2"),  //队列
//            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT), //交换机
//            key = {"yellow","red"} //路由key
//    ))
//    public void listenDirectQueue2(String msg) throws Exception {
//        log.warn("消费者2接收到direct消息：【" + msg + "】" +"---->"+ LocalTime.now());
//        Thread.sleep(300);
//    }



    //topic
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue("topic.Queue1"),  //队列
//            exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC), //交换机
//            key = {"china.#"} //路由key
//    ))
//    public void listenTopicQueue1(String msg) throws Exception {
//        log.info("消费者1接收到topic消息：【" + msg + "】" +"---->"+ LocalTime.now());
//        Thread.sleep(300);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue("topic.Queue2"),  //队列
//            exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC), //交换机
//            key = {"#.news"} //路由key
//    ))
//    public void listenTopicQueue2(String msg) throws Exception {
//        log.warn("消费者2接收到topic消息：【" + msg + "】" +"---->"+ LocalTime.now());
//        Thread.sleep(300);
//    }


    //消息转换器
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue2(Map map) throws Exception {
        log.warn("消费者接收到消息：【" + map + "】" +"---->"+ LocalTime.now());
    }
}
